package frameworks.widgets
{
	import flash.events.EventDispatcher;
	import frameworks.events.ComponentEvt;
	
	public class BaseStack extends EventDispatcher
	{
		protected var _list:Array = [];
		protected var _curItem:BaseStackItem = null;
		
		public function BaseStack()
		{
		}
		
		public function get curItem():BaseStackItem{return _curItem;}
		public function get list():Array{return _list;}
		
		protected function validityItem(item:BaseStackItem):Boolean{
			return true;
		}
		
		public function addItem(item:BaseStackItem):void{
			if(validityItem(item)){
				item.stack = this;
				_list.push(item);
				if(_curItem == null){
					this.dispatchEvent(new ComponentEvt(EventType.BusyStart,_list));
					nextItem();
				}
			}
		}
		
		private function nextItem():void{
			if(_list.length>0){
				_curItem = _list.shift();
				this.dispatchEvent(new ComponentEvt(EventType.ItemStart,_curItem));
				_curItem.execute();
			}else{
				_curItem = null;
				this.dispatchEvent(new ComponentEvt(EventType.BusyEnd,_list));
			}
			
		}
		public function progressItem(data:Object):void{
			this.dispatchEvent(new ComponentEvt(EventType.ItemProgress,data));
		}
		
		public function completeItem(result:Object=null):void{
			dispatchEvent(new ComponentEvt(EventType.ItemEnd,_curItem));
			nextItem();
		}
		public function errorItem(result:Object=null):void{
			dispatchEvent(new ComponentEvt(EventType.ItemError,_curItem));
			nextItem();
		}
		public function successItem(result:Object=null):void{
			dispatchEvent(new ComponentEvt(EventType.ItemSuccess,_curItem));
			nextItem();
		}
	}
}